/**
 * 
 */
var callback=1;
var totalNum=0;
var t_id;
function TeacherList(pageNo){
	$.ajax({
		url:basePath+'/teacher/teacherList',
		data:{'pageNo':pageNo,'pageSize':10},
		dataType:'json',
		type:'post',
		success:function(data)
		{
			 $("#Ttbody").empty();
		     
		  	 var count=totalNum;
			 totalNum=data.json.total/10;
			 if(count!=totalNum)
			     initPagination();
			 var status=["教师","班主任"];
				for(var i=0;i<data.json.TeacherList.length;i++)
			          $("#Ttbody").append('<tr><td>'+data.json.TeacherList[i].tid+'</td><td>'+data.json.TeacherList[i].tusername+'</td>'
			        		 +'<td>'+status[data.json.TeacherList[i].tlevel-1]+'</td>'
			        		 +'<td>'+data.json.TeacherList[i].ttelnumber+'</td>'
			        		 +'<td>  <div class="hidden-sm hidden-xs action-buttons">'
	                         +  '<a class="green" href="#"> <a href="#modal-table" role="button" class="green" data-toggle="modal"'
	                         +'onclick="teacherinfo(\''+data.json.TeacherList[i].tid+'\',\''+data.json.TeacherList[i].tlevel+'\',\''+data.json.TeacherList[i].ttelnumber+'\',\''+data.json.TeacherList[i].tusername+'\')">'
	                         +  '<i class="ace-icon fa fa-pencil bigger-130"></i></a> </a>  <a class="red" href="#">'
	                         + '<a href="javascript:void(0);" onclick="del(\''+data.json.TeacherList[i].tid+'\')"><i class="ace-icon fa fa-trash-o bigger-130"></i></a></a>'
	                         +' </div>  </td></tr>');
			}
	});
}

function initPagination() {
	//var num_entries = $("#hiddenresult div.result").length;
	// 创建分页
	$("#Pagination").pagination(totalNum, {
		num_edge_entries: 1, //边缘页数
		num_display_entries: 4, //主体页数
		callback: pageselectCallback,
		items_per_page: 1, //每页显示1项
		prev_text: "前一页",
		next_text: "后一页"
	});
 }

function pageselectCallback(page_index, jq){
	/*	var new_content = $("#hiddenresult div.result:eq("+page_index+")").clone();
		$("#Searchresult").empty().append(new_content); //装载对应分页的内容
		
*/	
         page_index+=1;    

        	 (callback==1)? callback=0:TeacherList(page_index); 
		return false;
		}

function teacherinfo (tid,tlevel,ttel,tname)
{
	t_id=tid;
	$("#Btbody").empty();
	$("#Btbody").append(' <tr><td><input class="input-small" id="tname" value="'+tname+'"/></td>'
           +'<td><input class="input-small" id="tlevel" value="'+tlevel+'"/></td>'
            +'<td><input class="input-small" id="ttel" value="'+ttel+'"/></td></tr>');
}

function updateTeacher()
{
	var tname=$("#tname").val();
	 var tlevel=$("#tlevel").val();
	 var ttel=$("#ttel").val();
	  var teacherUpinfo=t_id+","+tname+","+tlevel+","+ttel;
	$.ajax({
		url:basePath+'/admin/TeacherAction!updateTeacher',
		type:'post',
		data:{"tinfo":teacherUpinfo},
		dataType:'json',
		success:function()
		{
			toastr.success("更新成功！");
			location.href=basePath+"/admin/dataTeacher";
		},
		error:function()
		{
			toastr.error("500:服务器错误！");
		}
		
		
	});
	}

function del(id)
{
	if(confirm("确认删除？"))
	$.ajax({
		url:basePath+'/admin/TeacherAction!deleteTeacher',
		type:'post',
		data:{"t_id":id},
		dataType:'json',
		success:function()
		{
			toastr.success("删除成功！");
		},
		error:function()
		{
			toastr.error("该教师已任课不能删除！");
		}
		
		
	});
	
}
TeacherList(1);